package com.violet.service.app.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.violet.domain.emuns.OrderStatus;
import com.violet.domain.po.BuyOrder;
import com.violet.domain.result.ResultPage;
import com.violet.domain.search.MyPage;
import com.violet.domain.vo.app.AOrderVo;
import com.violet.mapper.BuyOrderMapper;
import com.violet.service.app.AWaitReceivingOrderService;
import com.violet.utils.OrderUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AWaitReceivingOrderServiceImpl extends ServiceImpl<BuyOrderMapper, BuyOrder>
        implements AWaitReceivingOrderService {

    private final OrderUtils orderUtils;

    @Override
    public ResultPage<List<AOrderVo>> getWaitReceivingOrders(MyPage myPage) {
        ResultPage<List<AOrderVo>> page = orderUtils.getOrder(myPage, OrderStatus.WAIT_RECEIVING);
        // 预计送达时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        page.getData().forEach(vo -> {
            LocalDateTime parse = LocalDateTime.parse(vo.getOrderTime(), formatter);
            // 下单时间 + 2 小时
            LocalDateTime minutes = parse.plusMinutes(120);
            // 再拿到其时分秒
            vo.setEstimatedDeliveryTime("今天: " + minutes.getHour() + ":" + minutes.getMinute());
            // 比如下单时间是: 2024-01-01 12:00:00
            // 那么预计送达时间就显示: 预计今天 13:30 送达
        });
        return page;
    }

    @Override
    public Boolean confirmReceivingOrder(String id) {
        return orderUtils.confirmReceivingOrder(id);
    }
}
